// pages/member/supplier/SupInfoManage/SupInfoManage.js
var app = getApp();
Page({

  /**
	 * 页面的初始数据
	 */
  data: {
    member_id: '',
    information: {
      my_name: '',
      phone_number: '',
      store_name: '',
      store_logo: '',
      bank_account: '',
      owner_name: '',
      opening_bank: '',
      opening_branch: '',
      enterprise_alipay: '',
      enterprise_alipay_user: '',
      alipay: '',
      alipay_username: '',
      weChat_account: '',
      category:'',
      group:'',
      imageList:''
    },
    imageUrl: '',
    jumpState: true,
    fill_set: '',
    // imageList:[]
  },

  /**
	 * 生命周期函数--监听页面加载
	 */
  onLoad: function(options) {
    if (options.member_id) {
      this.setData({
        member_id: options.member_id
      });
    }
    this._getFillSet();
    this._getJson();
  },

  /**
	 * 生命周期函数--监听页面初次渲染完成
	 */
  onReady: function() {

  },

  /**
	 * 生命周期函数--监听页面显示
	 */
  onShow: function() {

  },

  /**
	 * 生命周期函数--监听页面隐藏
	 */
  onHide: function() {

  },

  /**
	 * 生命周期函数--监听页面卸载
	 */
  onUnload: function() {

  },

  /**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
  onPullDownRefresh: function() {

  },

  /**
	 * 页面上拉触底事件的处理函数
	 */
  onReachBottom: function() {

  },

  /**
	 * 用户点击右上角分享
	 */
  onShareAppMessage: function() {

  },
  _getFillSet() {
    let that = this;
    let urlStr = app.getNetAddresss("plugin.supplier.frontend.info.getFillSet");
    app._postNetWork({
      url: urlStr,
      showToastIn: false,
      data: {},
      success: function(resdata) {
        let res = resdata.data;
        if (res.result == 1) {
          that.setData({
            'fill_set': res.data.fill_set ? res.data.fill_set : ''
          });
          console.log(that.data.fill_set);
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }

      },
      fail: function(res) {
        console.log(res.msg);
      }
    });
  },
  //获取数据
  _getJson() {
    let that = this;
    let urlStr = app.getNetAddresss("plugin.supplier.frontend.info.index");
    app._postNetWork({
      url: urlStr,
      showToastIn: false,
      data: {
        member_id: that.data.member_id
      },
      success: function(resdata) {
        var res = resdata.data;
        console.log(resdata,'resdata');
        if (res.result == 1) {
          that.setData({
            'information.my_name': res.data.realname ? res.data.realname : '',
            'information.phone_number': res.data.mobile ? res.data.mobile : '',
            'information.store_name': res.data.store_name ? res.data.store_name : "",
            'information.store_logo': res.data.logo ? res.data.logo : '',
            'imageUrl': res.data.logo ? res.data.logo : '',
            'information.bank_account': res.data.company_bank ? res.data.company_bank : '',
            'information.owner_name': res.data.bank_username ? res.data.bank_username : '',
            'information.opening_bank': res.data.bank_of_accounts ? res.data.bank_of_accounts : '',
            'information.opening_branch': res.data.opening_branch ? res.data.opening_branch : '',
            'information.enterprise_alipay': res.data.company_ali ? res.data.company_ali : '',
            'information.enterprise_alipay_user': res.data.company_ali_username ? res.data.company_ali_username : '',
            'information.alipay': res.data.ali ? res.data.ali : '',
            'information.alipay_username': res.data.ali_username ? res.data.ali_username : '',
            'information.weChat_account': res.data.wechat ? res.data.wechat : '',
            'information.category': res.data.has_one_category ? res.data.has_one_category.title : '默认行业',
            'information.group': res.data.has_one_group ? res.data.has_one_group.title : '默认分组',
            'information.imageList':res.data.has_one_aptitude.aptitude ? res.data.has_one_aptitude.aptitude : '',
          });
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }

      },
      fail: function(res) {}
    });
  },
  //真实姓名值绑定
  myNameinp(e) {
    let val = e.detail.value;
    this.setData({
      'information.my_name': val
    });
  },
  //手机号码值绑定
  phoneNumberinp(e) {
    let val = e.detail.value;
    this.setData({
      'information.phone_number': val
    });
  },
  //店铺名称值绑定
  storeNameinp(e) {
    let val = e.detail.value;
    this.setData({
      'information.store_name': val
    });
  },
  //银行卡账号值绑定
  bankAccountinp(e) {
    let val = e.detail.value;
    this.setData({
      'information.bank_account': val
    });
  },
  //开户人姓名值绑定
  ownerNameinp(e) {
    let val = e.detail.value;
    this.setData({
      'information.owner_name': val
    });
  },
  //开户银行值绑定
  openingBankinp(e) {
    let val = e.detail.value;
    this.setData({
      'information.opening_bank': val
    });
  },
  //开户支行值绑定
  openingBranchinp(e) {
    let val = e.detail.value;
    this.setData({
      'information.opening_branch': val
    });
  },
  //企业支付宝账号值绑定
  enterpriseAlipayinp(e) {
    let val = e.detail.value;
    this.setData({
      'information.enterprise_alipay': val
    });
  },
  //企业支付宝用户名值绑定
  enterpriseAlipayUserinp(e) {
    let val = e.detail.value;
    this.setData({
      'information.enterprise_alipay_user': val
    });
  },
  //支付宝账号值绑定
  alipayinp(e) {
    let val = e.detail.value;
    this.setData({
      'information.alipay': val
    });
  },
  //支付宝用户名值绑定
  alipayUsernameinp(e) {
    let val = e.detail.value;
    this.setData({
      'information.alipay_username': val
    });
  },
  weChatAccountinp(e) {
    let val = e.detail.value;
    this.setData({
      'information.weChat_account': val
    });
  },
  //选择图片
  onRead() {
    let that = this;
    let wx_token = wx.getStorageSync('wx_token');
    let session = wx.getStorageSync('session');
    let urlStr = app.getNetAddresss("plugin.supplier.frontend.info.upload");
    urlStr += '&3rd_session=' + session;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        const tempFilePaths = res.tempFilePaths;
        console.log(tempFilePaths[0]);
        wx.compressImage({
          src: tempFilePaths[0], // 图片路径
          quality: 60, // 压缩质量
          success(e) {
            let photourl = e.tempFilePath;
            console.log('eee', e);
            wx.uploadFile({
              url: urlStr,
              filePath: photourl,
              name: 'file',
              header:{
                "Content-Type": "multipart/form-data",
                Cookie: "PHPSESSID=" + wx_token,
              },
              success(resdata) {
                var res = JSON.parse(resdata.data);
                if (res.result == 1) {
                  that.setData({
                    imageUrl: photourl,
                    'information.store_logo': res.data.img
                  });
                } else {
                  wx.showToast({
                    icon: 'none',
                    title: res.msg,
                    duration: 1500
                  });
                }
              }
            });
          },
          fail(e) {

          },
          complete(e) {

          }
        });
      }
    });
  },
  //确认条件
  jumpJudge() {
    this.setData({
      jumpState: true
    });

    if (!this.data.information.my_name || !this.data.information.phone_number || !this.data.information.store_name || !this.data.information.store_logo) {
      this.setData({
        jumpState: false
      });
    }

    // for (let item in this.data.information) {
    // 	if (this.data.information[item] == '') {
    // 		this.setData({
    // 			jumpState: false
    // 		})
    // 	}
    // }

    if (this.data.fill_set) {
      if (this.data.fill_set.company_bank == "1") {
        if (this.data.information.bank_account == "") {
          this.setData({
            jumpState: false
          });
        }
      }
      if (this.data.fill_set.bank_username == "1") {
        if (this.data.information.owner_name == "") {
          this.setData({
            jumpState: false
          });
        }
      }
      if (this.data.fill_set.bank_of_accounts == "1") {
        if (this.data.information.opening_bank == "") {
          this.setData({
            jumpState: false
          });
        }
      }
      if (this.data.fill_set.opening_branch == "1") {
        if (this.data.information.opening_branch == "") {
          this.setData({
            jumpState: false
          });
        }
      }
      if (this.data.fill_set.company_ali == "1") {
        if (this.data.information.enterprise_alipay == "") {
          this.setData({
            jumpState: false
          });
        }
      }
      if (this.data.fill_set.company_ali_username == "1") {
        if (this.data.information.enterprise_alipay_user == "") {
          this.setData({
            jumpState: false
          });
        }
      }
      if (this.data.fill_set.ali == "1") {
        if (this.data.information.alipay == "") {
          this.setData({
            jumpState: false
          });
        }
      }
      if (this.data.fill_set.ali_username == "1") {
        if (this.data.information.alipay_username == "") {
          this.setData({
            jumpState: false
          });
        }
      }
      if (this.data.fill_set.wechat == "1") {
        if (this.data.information.weChat_account == "") {
          this.setData({
            jumpState: false
          });
        }
      }
    }

    if (this.data.imageUrl == '') {
      this.setData({
        jumpState: false
      });
    }
  },
  //确认修改
  modifyInformation() {
    var that = this;
    that.jumpJudge();
    if (that.data.jumpState) {
      if (app._isMoblie(that.data.information.phone_number)) {
        wx.showToast({
          icon: 'none',
          title: '请输入正确的手机号',
          duration: 1500
        });
        return;
      }
      if (this.data.fill_set.company_bank == '1') {
        if (!/^[0-9]*$/.test(that.data.information.bank_account)) {
          wx.showToast({
            icon: 'none',
            title: '请输入正确的银行卡账号',
            duration: 1500
          });
          return;
        }
      }

      if (this.data.fill_set.company_ali == '1') {
        if (!/^[a-zA-Z\d._-]*\@[a-zA-Z\d.-]{1,10}\.[a-zA-Z\d]{1,20}$/.test(that.data.information.enterprise_alipay)) {
          wx.showToast({
            icon: 'none',
            title: '请输入正确的企业支付宝账号',
            duration: 1500
          });
          return;
        }
      }

      if (this.data.fill_set.ali == '1') {
        if (!/^(?:1[3-9]\d{9}|[a-zA-Z\d._-]*\@[a-zA-Z\d.-]{1,10}\.[a-zA-Z\d]{1,20})$/.test(that.data.information.alipay)) {
          wx.showToast({
            icon: 'none',
            title: '请输入正确的支付宝账号',
            duration: 1500
          });
          return;
        }
      }

      if (this.data.fill_set.wechat == '1') {
        if (!/^[a-zA-Z]([-_a-zA-Z0-9]{5,19})+$/.test(that.data.weChat_account)) {
          wx.showToast({
            icon: 'none',
            title: '请输入正确的微信号',
            duration: 1500
          });
          return;
        }
      }

      let json = {
        realname: that.data.information.my_name,
        mobile: that.data.information.phone_number,
        store_name: that.data.information.store_name,
        logo: that.data.information.store_logo,
        company_bank: that.data.information.bank_account,
        bank_username: that.data.information.owner_name,
        bank_of_accounts: that.data.information.opening_bank,
        opening_branch: that.data.information.opening_branch,
        company_ali: that.data.information.enterprise_alipay,
        company_ali_username: that.data.information.enterprise_alipay_user,
        ali: that.data.information.alipay,
        ali_username: that.data.information.alipay_username,
        wechat: that.data.information.weChat_account,
      };
      let urlStr = app.getNetAddresss("plugin.supplier.frontend.info.edit");
      app._postNetWork({
        url: urlStr,
        showToastIn: false,
        data: json,
        success: function(resdata) {
          var res = resdata.data;
          if (res.result == 1) {
            wx.showToast({
              title: '修改成功',
              icon: 'none',
              duration: 1500,
              success: function() {
                setTimeout(function() {
                  //要延时执行的代码
                  wx.navigateBack({
                    delta: 1
                  });
                }, 1500); //延迟时间
              },
            });
          } else {
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }

        },
        fail: function(res) {}
      });
    } else {
      wx.showToast({
        icon: 'none',
        title: '填写信息不完整',
        duration: 1500
      });
    }
  },
});
